import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  // 重定向
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: () => import('@/views/login/Login.vue'),
  },
  // 首页
  {
    path: '/home',

    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        // 等价于去父亲的path
        path: '',
        component: () => import('@/views/home/Home.vue')

      }
    ]
  },
  // 账号管理
  {
    path: '/account',
    meta: {
      title: '账号管理'
    },
    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        path: '/account/list',
        meta: {
          title: '账号列表'
        },
        component: () => import('@/views/account/AccountList.vue')
      },
      {
        path: '/account/add',
        meta: {
          title: '账号添加'
        },
        component: () => import('@/views/account/AccountAdd.vue')
      },
      {
        path: '/account/center',
        meta: {
          title: '个人中心'
        },
        component: () => import('@/views/account/AccountCenter.vue')
      },
      {
        path: '/account/edit',
        meta: {
          title: '修改密码 '
        },
        component: () => import('@/views/account/AccountEdit.vue')
      },

    ]
  },
  // 商品管理
  {
    path: '/goods',
    meta: {
      title: '商品管理'
    },
    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        path: '/goods/list',
        meta: {
          title: '商品列表'
        },
        component: () => import('@/views/goods/GoodsList.vue')
      },
      {
        path: '/goods/add',
        meta: {
          title: '商品添加'
        },
        component: () => import('@/views/goods/GoodsAdd.vue')
      },
      {
        path: '/goods/type',
        meta: {
          title: '商品分类'
        },
        component: () => import('@/views/goods/GoodsType.vue')
      },

    ]
  },
  // 订单管理
  {
    path: '/order',
    meta: {
      title: '订单管理'
    },
    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        path: '',
        component: () => import('@/views/order/Order.vue')
      }
    ]
  },

  // 店铺管理
  {
    path: '/shop',
    meta: {
      title: '店铺管理'
    },
    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        path: '',
        component: () => import('@/views/shop/Shop.vue')
      }
    ]
  },

  // 销售统计
  {
    path: '/sale',
    meta: {
      title: '销售统计'
    },
    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        path: '/sale/goods',
        meta: {
          title: '商品统计'
        },
        component: () => import('@/views/sale/GoodsSale.vue')
      },
      {
        path: '/sale/order',
        meta: {
          title: '订单统计'
        },
        component: () => import('@/views/sale/OrderSale.vue')
      },

      {
        path: '/sale/total',
        meta: {
          title: '订单统计'
        },
        component: () => import('@/views/sale/TotalSale.vue')
      }
    ]
  },
  // 404页面
  {
    path: '/*',
    component: () => import('@/components/Vue404.vue')
  }
]

// 静态路由
const constRoutes = [
  {
    path: '/',
    redirect: '/home'
  },
  // 登录
  {
    path: '/login',
    component: () => import('@/views/login/Login.vue')
  },
  // 首页
  {
    path: '/home',
    component: () => import('@/layout/Layout.vue'),
    // meta: {title: '首页'},
    children: [
      {
        // 等价于取父亲的path
        path: '',
        // meta: {title: '首页'},
        component: () => import('@/views/home/Home.vue')

      }
    ]
  },
]
// 动态路由
// 普通管理员访问的路由
const normalRoutes = [
  {
    path: '/account',
    component: () => import('@/layout/Layout.vue'),
    meta: { title: '账号管理' },
    children: [
      {
        path: '/account/list',
        meta: { title: '账号列表' },
        component: () => import('@/views/account/AccountList.vue')
      },

      {
        path: '/account/center',
        meta: { title: '个人中心' },
        component: () => import('@/views/account/AccountCenter.vue')
      }
    ]
  },

  // 商品管理
  {
    path: '/goods',
    meta: {
      title: '商品管理'
    },
    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        path: '/goods/list',
        meta: {
          title: '商品列表'
        },
        component: () => import('@/views/goods/GoodsList.vue')
      },

      {
        path: '/goods/type',
        meta: {
          title: '商品分类'
        },
        component: () => import('@/views/goods/GoodsType.vue')
      },

    ]
  },
]

// 超级管理员访问的路由
const superRoutes = [
  // 账号
  {
    path: '/account',
    component: () => import('@/layout/Layout.vue'),
    meta: { title: '账号管理' },
    children: [
      {
        path: '/account/list',
        meta: { title: '账号列表' },
        component: () => import('@/views/account/AccountList.vue')
      },
      {
        path: '/account/add',
        // name: '账号添加',
        meta: { title: '账号添加' },
        component: () => import('@/views/account/AccountAdd.vue')
      },
      {
        path: '/account/edit',
        meta: { title: '密码修改' },
        component: () => import('@/views/account/AccountEdit.vue')
      },
      {
        path: '/account/center',
        meta: { title: '个人中心' },
        component: () => import('@/views/account/AccountCenter.vue')
      }
    ]
  },
  // 商品
  {
    path: '/goods',
    component: () => import('@/layout/Layout.vue'),
    meta: { title: '商品管理' },
    children: [
      {
        path: '/goods/list',
        meta: { title: '商品列表' },
        component: () => import('@/views/goods/GoodsList.vue')
      },
      {
        path: '/goods/add',
        meta: { title: '商品添加' },
        component: () => import('@/views/goods/GoodsAdd.vue')
      },
      {
        path: '/goods/type',
        meta: { title: '商品分类' },
        component: () => import('@/views/goods/GoodsType.vue')
      }
    ]
  },
  // 订单
  {
    path: '/order',
    component: () => import('@/layout/Layout.vue'),
    meta: { title: '订单管理' },
    children: [
      {
        path: '',
        // meta: {title: '订单管理'},
        component: () => import('@/views/order/Order.vue'),
        // 路由的独享守卫
        beforeEnter: (to, from, next) => {
          next()
        }
      }
    ]
  },
  // 店铺
  {
    path: '/shop',
    component: () => import('@/layout/Layout.vue'),
    meta: { title: '店铺管理' },
    children: [
      {
        path: '',
        // meta: {title: '店铺信息'},
        component: () => import('@/views/shop/Shop.vue')
      }
    ]
  },
  // 销售
  {
    path: '/sale',
    component: () => import('@/layout/Layout.vue'),
    meta: { title: '销售统计' },
    children: [
      {
        path: '/sale/goods',
        meta: { title: '商品统计' },
        component: () => import('@/views/sale/GoodsSale.vue')
      },
      {
        path: '/sale/order',
        meta: { title: '订单统计' },
        component: () => import('@/views/sale/OrderSale.vue')
      },
      {
        path: '/sale/total',
        meta: { title: '全统计' },
        component: () => import('@/views/sale/TotalSale.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes: constRoutes
})


export function addRoutes() {

  let role = localStorage.getItem('role')
  if (!role) return

  if (role === 'super') {

    superRoutes.forEach(route => {

      router.addRoute(route)
    })
  } else {
    // 添加normal对应的路由
    normalRoutes.forEach(route => {
      router.addRoute(route)
    })
  }
}

addRoutes()



// 路由守卫
// 路由白名单 不需要token可以跳转
let whileList = ['/login', 'info']

router.beforeEach((to, from, next) => {

  if (whileList.includes(to.fullPath)) return next()

  let token = localStorage.getItem('token')

  if (token) {
    next()
  } else {
    next('/login')

  }
})

export default router
